// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'babel-polyfill'
import Vue from 'vue'
import vuet from '../vuet'
import VueRouter from 'vue-router'
import store from '../vuex'
import * as routers from '../routers'
import * as filters from '../filters'
import FastClick from '../assets/dep/fastclick'
import * as request from '../request'
import * as utils from '../utils'
import 'mint-ui/lib/style.css'
import {Toast, MessageBox, Indicator} from 'mint-ui'
import config from '../config'
import * as dictionary from '../dictionary'
import native from '../native'
import * as storage from '../storage'
import * as validator from '../validator'
import * as rem from '../rem'
import * as PromiseExtend from '../PromiseExtend'
import RegistCouponPop from '../components/registCoupon'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
require('../assets/style/iconfont.css')
require('../assets/style/global.css')
require('../assets/style/css.css')

Vue.use(ElementUI)
Vue.use(VueRouter)
var components = require('../components')

// 去除手机的300毫秒点击延迟
FastClick.attach(document.body)

rem.init() // 初始化rem单位
PromiseExtend.init() // 初始化Promise的扩展
components.regist(Vue) // 全局注册组件

// 挂载全局参数与方法
Vue.prototype.$deviceType = utils.getDeviceType()
Vue.prototype.$request = request
Vue.prototype.$utils = utils
Vue.prototype.$toast = Toast
Vue.prototype.$indicator = Indicator
Vue.prototype.$messageBox = MessageBox
Vue.prototype.$config = config
Vue.prototype.$dictionary = dictionary
Vue.prototype.$native = native
Vue.prototype.$storage = storage
Vue.prototype.$validator = validator
Vue.prototype.$filters = filters
// 实例化Vue的filter
Object.keys(filters).forEach(k => Vue.filter(k, filters[k]))

// 实例化VueRouter
const router = new VueRouter({
  mode: 'history',
  base: '/wap/',
  routes: routers.list,
  scrollBehavior: routers.scrollBehavior
})

storage.setEntryUrl(window.location.href) // 设置当前入口地址

// 为保证hbuilder的plus对象在使用时已初始化，在其初始化完成后再启动应用
// document.addEventListener('plusready', function () {
//   new Vue({
//     router,
//     store
//   }).$mount('#app')
// })
// if (config.nativeType == 'pc') {
//   storage.setKey('19607a26ac02059db99b8776d038fc15') // 测试用
// }
let member_ids = ''
console.log(native.getType())
router.beforeEach(async (to, from, next) => {
  if (to.query.fkmid) {
    window.localStorage.setItem('fkmid', to.query.fkmid)
  }
  console.log('这里是beforeEach', to.fullPath)
  let url = config.website + '/wap' + to.fullPath
  native.weixinShare(url)
  if (to.meta.login) { // 需要先登录才能访问的页面
    let key = storage.getKey()
    console.log(key)
    // 需要的登录后才能访问
    if (!key || key == '' || key == 'null') {
      key = await request.getKey()
      if (key) {
        storage.setKey(key)
        if (!storage.getMemberInfo()) {
          let memberInfo = await request.getMemberInfo()
          storage.setMemberInfo(memberInfo)
        }
        next()
      } else {
        if (utils.getDeviceType() == 'wx-Android' || utils.getDeviceType() == 'wx-iPhone') {
          native.weixinLogin() // 微信授权后才能取到key
          return
        } else {
          next({name: 'login-password'})
        }
      }
    } else {
      if (!storage.getMemberInfo()) {
        let memberInfo = await request.getMemberInfo()
        storage.setMemberInfo(memberInfo)
      }
    }
    if (storage.getMemberInfo().member_id) {
      member_ids = storage.getMemberInfo().member_id
    }
  }
  console.log(member_ids)

  to.params.member_id = member_ids
  if (to.meta.auth) { // 需要完善资料后才能访问的页面
    // let auth = storage.getAuthed()
    // if (!auth || auth == '' || auth == 'null') {
    //   let authStatus = await request.getAuth()
    //   if (authStatus != '1') { // 未完善过
    //     let query = to.query
    //     query.to = to.name
    //     next({name: 'modify-mobilenext', query: query, params: to.params})
    //   } else {
    //     storage.setAuthed(true)
    //     next()
    //   }
    // }
    let authStatus = await request.getAuth()
    if ((authStatus != '1' && utils.getDeviceType() == 'wx-Android') || (authStatus != '1' && utils.getDeviceType() == 'wx-iPhone') || (member_ids == 100189 && utils.getDeviceType() == 'wx-Android')) { // 未完善过
      let query = to.query
      query.to = to.name
      MessageBox.alert('请先绑定手机号码！', '提示').then(action => {
        next({name: 'modify-mobilenext', query: query, params: to.params, replace: true})
      })
    } else {
      storage.setAuthed(true)
      next()
    }
  }
  next()
})

router.afterEach((to, from) => {
  utils.removeBodyFix() // 去除body的滚动固定
  setTimeout(() => {
    native.weixinShare(window.location.href) // 每个页面需要重新进行微信配置
    if (to.query.registFlag == 'new') {
      RegistCouponPop()
    }
    if (to.name == 'goods-detail') {
      console.log('goodsCommonId', to.params.id)
      storage.setCurrentGoodsCommonId(to.params.id)
    } else {
      storage.delCurrentGoodsCommonId()
    }
  }, 200)
})

// 全局注入混合
Vue.mixin({
  mounted() {
    window.scrollTo(1, 1) // 移动1px，解决浏览器后退时，需要滚动下页面才会渲染页面的bug
  }
})

new Vue({
  router,
  vuet,
  store
}).$mount('#app')
